/*
Copyright  2018 Blockchain Technology and Application Joint Lab, Fintech Research Center of ISCAS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import {Users} from '../imports/api/users/users.js';
import {CopyrightCertificates} from '../imports/api/copyright_certificates/copyright_certificates.js';
import {Meteor} from 'meteor/meteor';
import Deasync from 'deasync';

let faker = require('faker/locale/zh_CN');

const count_user = 10;
const count_copyright_certificate = 5;
const count_unconfirm = 2;
let fus=[];

export function fake_user(){
    return fus[0];
}
export function fake() {
    //data exists, no need for fake
    fus = Users.find().fetch();
    if (fus.length > 0) {
        return fus[0];
    }
     // random credential_number
     function randomCredentialNumber(length){
        if(typeof length === 'undefined'){
            length = 18;
        }
        let credential_number = "";
        for(let i = 0; i < length; i++) {
            credential_number += faker.random.number(9);
        }
        return credential_number;
     }

    for(var i=0; i<count_user; i++){ 
        fus[i]={
                phone: faker.phone.phoneNumber(),
                account_address: faker.finance.bitcoinAddress(),
                credential_type: faker.random.number({min: 1, max: 8}),
                credential_number: randomCredentialNumber(),
                name: faker.name.findName(),
                email: faker.internet.email(),
                createdAt: faker.date.recent(),
                avatar: faker.image.avatar(),
        };
        const addr = faker.address.state() + ',' + faker.address.city()
                        + ',' + faker.address.streetAddress();
        const unit = faker.company.companyName();
        fus[i].address = addr;
        fus[i].unit = unit;
        if(i % 2 == 0)
            fus[i].status = true;
        let join_result;
        Meteor.call('accountAdd.joinForNew', fus[i].phone, fus[i].phone, fus[i], (err, result) => {
            if(err)
                console.log('Join new user failed: ', err);
            else{
                join_result = result;
            }
        });
        Deasync.loopWhile(() => join_result === undefined);
    }
    
    
    // random hash hex code
    function random_hash(length){
        if(typeof length === 'undefined')
            length = 64;
        let hash = '';
        for(var i = 0; i < length - 1; i++){
            hash += faker.random.arrayElement(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
             'A', 'B', 'C', 'D', 'E', 'F']);
        }
        return hash;
    }

    let fccs=[];
    fus.forEach((userf) => {
        for(var i=0; i<count_copyright_certificate; i++){
            let c_number = random_hash();
            let owner_account = userf.account_address;
            let status = true;
            let timestamp = parseInt(Date.now() / 1000);
            if(i < count_unconfirm){
            //留一些待确认
                status = false;
                timestamp = -1;
            }
            let hash_algorithm = {name: faker.random.arrayElement(['SHA256', 'RSA', 'MD5']), parameter: faker.lorem.words()};
            hash_algorithm = JSON.stringify(hash_algorithm);

            let pic_identity = random_hash();

            let pic_desc = {author: faker.name.findName(), name: faker.lorem.word(), 
                file_size: faker.random.number(), desc: faker.lorem.sentences(),
                 picture_url: faker.image.image(350, 240, true)};
            pic_desc = JSON.stringify(pic_desc);

            let createdAt = new Date();

            let fcc = {
                certificate_number: c_number,
                owner_account_address: owner_account,
                status: status,
                block_timestamp: timestamp,
                hash_algorithm: hash_algorithm,
                picture_digital_identity: pic_identity,
                picture_desc: pic_desc,
                createdAt: createdAt,
            };
            fccs.push(fcc);
        }
    });
   
    return fus[0];
}   
